package com.lanou.musicproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.Mv;
import com.lanou.musicproject.tools.ImageUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/22.
 * Mv适配器
 */
public class MvAdapter extends BaseAdapter {
    //获取实体类中thumbnail2(图片),title(标题),artist(艺术家)
    private List<Mv.ResultEntity.MvListEntity> data;
    //上下文
    private Context mContext;

    //有参构造方法
    public MvAdapter(Context c, List<Mv.ResultEntity.MvListEntity> data) {
        this.data = data;
        mContext = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (data != null && data.size() > 0)
            return data.size();
        else
            return 0;
    }

    //获得当前行布局的坐标
    @Override
    public Mv.ResultEntity.MvListEntity getItem(int position) {
        if (data != null && data.size() > 0)
            return data.get(position);
        else
            return null;
    }

    //获得当前行布局ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    //获得行布局内容
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.mv_gridview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Mv.ResultEntity.MvListEntity mv = getItem(position);
        if (mv != null) {
            Picasso.with(mContext).load(mv.getThumbnail2()).into(holder.backgroundIV);
            holder.songTitleTV.setText(mv.getTitle());
            holder.songTagTV.setText(mv.getArtist());
        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'mv_gridview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.backgroundIV)
        ImageUtils backgroundIV;
        @Bind(R.id.songTitleTV)
        TextView songTitleTV;
        @Bind(R.id.songTagTV)
        TextView songTagTV;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
